#!/bin/sh

if [ "$1" == "--help" ] ; then
	echo
	echo " ------------------------------------------------------------------------------"
	echo "  format: sh $0 <phpXYm|phpXYs> [--force]"
	echo " ------------------------------------------------------------------------------"
	echo "  phpXYm/phpXYs"
	echo "      - start from php52m/php52s"
	echo "      - phpXYm for 'multiple php'"
	echo "      - phpXYs for 'special php' for Kloxo-ST panel"
	echo "  --force"
	echo "      - for re-install"
	echo
	exit;
fi

if [ "$#" == 0 ] ; then
	echo
	echo "- For help, type '$0 --help'"
	echo
	exit
fi

base_name="${1}"

if [ "echo '${base_name}'|grep 'php'|grep 's'" == "" ] || [ "echo '${base_name}'|grep 'php'|grep 'm'" == "" ] ; then
	echo "* WARNING: only for phpXYm or phpXYs (change XY to 52 - 70)"
	exit
fi

## ST -- remove last char (default m or s)
src_name="${base_name%?}"

if [ "$(uname -m)" == "x86_64" ] ; then
	uname_m="64"
else
	uname_m=""
fi

x="- Disable certain modules (rename to .nonini) in 'php.d' and 'php-zts.d'"
echo ${x}
echo ${x} >>/opt/${base_name}/install.log

for i in dba dbase eaccelerator ioncube pdo_dblib pdo_odbc pdo_pgsql pgsql suhosin odbc mongo \
	apc apcu imagick interbase ioncube-loader memcache memcached z-memcached mssql debug \
	pdo_dblib pdo_firebird mysqlnd_ms mysqlnd_mysql mysqlnd_mysqli mysqlnd pdo_mysqlnd \
	xdebug xcache opcache lzf redis enchant wddx xml_wddx ; do

	for j in php.d php-zts.d ; do
		for k in '01-' '10-' '15-' '20-' '25-' '30-' '35-' '40-' '45-' '50-' '55-' \
				'60-' '65-' '70-' '75-' '80-' '85-' '90-' '95-' '' ; do
			if [ -f /opt/${base_name}/etc/${j}/${k}${i}.ini ] ; then
				## ST -- remove path in 'extension='
				sed -i 's:extension = /:extension=/:' /opt/${base_name}/etc/${j}/${k}${i}.ini
				sed -i 's:/usr/lib'${uname_m}'/php/modules::' /opt/${base_name}/etc/${j}/${k}${i}.ini
				## ST -- readd path in 'zend_extension='
				if [ "$(cat /opt/${base_name}/etc/${j}/${k}${i}.ini|grep 'zend_extension=/')" == "" ] ; then
					sed -i 's:zend_extension=:zend_extension=/opt/'${base_name}'/usr/lib'${uname_m}'/php/modules/:' /opt/${base_name}/etc/${j}/${k}${i}.ini
				else
					sed -i 's:zend_extension=/:zend_extension=/opt/'${base_name}'/usr/lib'${uname_m}'/php/modules/:' /opt/${base_name}/etc/${j}/${k}${i}.ini
				fi

				if [ "$(echo ${k}${i}|grep mysqlnd)" != "" ] ; then
					## ST -- using mysqlnd instead mysql/mysqli in php54s
					x="-- Enable 'mysqlnd' instead 'mysql' modules in '/etc/${base_name}/etc/${j}'"
					echo $x
					echo $x >>/opt/${base_name}/install.log

					if [ -f /opt/${base_name}/etc/${j}/${k}mysql.ini ] ; then
						'mv' -f /opt/${base_name}/etc/${j}/${k}mysql.ini /opt/${base_name}/etc/${j}/${k}mysql.nonini
					fi

					if [ -f /opt/${base_name}/etc/${j}/${k}mysqli.ini ] ; then
						'mv' -f /opt/${base_name}/etc/${j}/${k}mysqli.ini /opt/${base_name}/etc/${j}/${k}mysqli.nonini
					fi

					if [ -f /opt/${base_name}/etc/${j}/${k}pdo_mysqlnd.ini ] ; then
						if [ -f /opt/${base_name}/etc/php.d/${k}pdo_mysql.ini ] ; then		
							'mv' -f /opt/${base_name}/etc/${j}/${k}pdo_mysql.ini /opt/${base_name}/etc/${j}/${k}pdo_mysql.nonini
						fi
					fi
				else
					x="-- Disable '${i}' modules in '/etc/${base_name}/etc/${j}'"
					echo $x
					echo $x >>/opt/${base_name}/install.log

					'mv' -f /opt/${base_name}/etc/${j}/${k}${i}.ini /opt/${base_name}/etc/${j}/${k}${i}.nonini
					
					if [ -f /opt/${base_name}/etc/php.d/${k}xml_wddx.nonini ] ; then
						'mv' -f /opt/${base_name}/etc/${j}/${k}${i}.nonini /opt/${base_name}/etc/${j}/${k}${i}.ini					
					fi
				fi
			fi
		done
	done
done

cd /opt/${base_name}

mkdir -p /opt/${base_name}/custom >/dev/null 2>&1

x="- Copy certain files to '/opt/${base_name}/custom'"
echo ${x}
echo ${x} >>/opt/${base_name}/install.log

'rm' -f  /opt/${base_name}/custom/php5* >/dev/null 2>&1

kipath="/usr/local/lxlabs/kloxo/init"

if [ -f ${kipath}/custom.php.ini.base ] ; then
	'cp' -rf ${kipath}/custom.php.ini.base /opt/${base_name}/custom/php.ini >/dev/null 2>&1
else
	'cp' -rf ${kipath}/php.ini.base /opt/${base_name}/custom/php.ini >/dev/null 2>&1
fi

sed -i 's:__extension_dir__:/opt/'${base_name}'/usr/lib/php/modules:' /opt/${base_name}/custom/php.ini

if [ "$(echo ${base_name}|grep 'php'|grep 's')" == "" ] ; then
	sed -i 's:__disable_functions__::' /opt/${base_name}/custom/php.ini
	sed -i 's:__for_phpm__::' /opt/${base_name}/custom/php.ini
	sed -i 's:__for_phps__:;:' /opt/${base_name}/custom/php.ini
	sed -i 's:__session_save_path__:/var/lib/php/session:' /opt/${base_name}/custom/php.ini
else
	sed -i 's:__disable_functions__:;:' /opt/${base_name}/custom/php.ini
	sed -i 's:__for_phpm__:;:' /opt/${base_name}/custom/php.ini
	sed -i 's:__for_phps__::' /opt/${base_name}/custom/php.ini
	sed -i 's:__session_save_path__:/usr/local/lxlabs/kloxo/session:' /opt/${base_name}/custom/php.ini
fi

for i in php-cgi.sh php-cli.sh php-fpm.sh php-ls.sh ; do
	if [ -f ${kipath}/custom.${i}.base ] ; then
		'cp' -rf ${kipath}/custom.${i}.base /opt/${base_name}/custom/${i} >/dev/null 2>&1
	else
		'cp' -rf ${kipath}/${i}.base /opt/${base_name}/custom/${i} >/dev/null 2>&1
	fi

	sed -i 's:__phpm__:'${base_name}':' /opt/${base_name}/custom/${i} >/dev/null 2>&1
done

if [ "${base_name}" == "php52s" ] || [ "${base_name}" == "php52m" ] ; then
	if [ -f ${kipath}/custom.php52-fpm.conf.base ] ; then
		'cp' -rf ${kipath}/custom.php52-fpm.conf.base /opt/${base_name}/custom/php-fpm.conf >/dev/null 2>&1
	else
		'cp' -rf ${kipath}/php52-fpm.conf.base /opt/${base_name}/custom/php-fpm.conf >/dev/null 2>&1
	fi

	sed -i 's:/var/run/php-fpm.pid:/var/run/'${base_name}'-fpm.pid:' /opt/${base_name}/custom/php-fpm.conf >/dev/null 2>&1
else
	if [ -f ${kipath}/custom.php53-fpm.conf.base ] ; then
		'cp' -rf ${kipath}/custom.php53-fpm.conf.base /opt/${base_name}/custom/php-fpm.conf >/dev/null 2>&1
	else
		'cp' -rf ${kipath}/php53-fpm.conf.base /opt/${base_name}/custom/php-fpm.conf >/dev/null 2>&1
	fi

	sed -i 's:/var/run/php-fpm/php-fpm.pid:/var/run/php-fpm/'${base_name}'-fpm.pid:' /opt/${base_name}/custom/php-fpm.conf
fi

if [ -f ${kipath}/custom.php-fpm.init.base ] ; then
	pcfile="${kipath}/custom.php-fpm.init.base"
else
	pcfile="${kipath}/php-fpm.init.base"
fi

'cp' -f ${pcfile} /opt/${base_name}/custom/php-fpm.init >/dev/null 2>&1
sed -i 's:__custom_name__:'${1}':' /opt/${base_name}/custom/php-fpm.init

x="- Set .sh to 755"
echo ${x}
echo ${x} >>/opt/${base_name}/install.log

chmod 755 /opt/${base_name}/custom/*.sh

x="- Create .sh symlink"
echo ${x}
echo ${x} >>/opt/${base_name}/install.log

for i in cgi cli ls fpm ; do
	if [ -f /opt/${base_name}/custom/php-${i}.sh ] ;  then
		x="-- From '/opt/${base_name}/custom/php-${i}.sh' to '/usr/bin/${base_name}-${i}'"
		echo ${x}
		echo ${x} >>/opt/${base_name}/install.log

		'rm' -f /usr/bin/${base_name}-${i} >/dev/null 2>&1
		ln -sf /opt/${base_name}/custom/php-${i}.sh /usr/bin/${base_name}-${i} >/dev/null 2>&1
	fi
done

if [ "${uname_m}" == "64" ] ; then
	x="- Create symlink from /opt/${base_name}/usr/lib64 to /opt/${base_name}/usr/lib"
	echo ${x}
	echo ${x} >>/opt/${base_name}/install.log

	if [ -d /opt/${base_name}/usr/lib ] ; then
		ln -sf /opt/${base_name}/usr/lib64/php /opt/${base_name}/usr/lib/php >/dev/null 2>&1

		if [ -d /opt/${base_name}/usr/lib64/php-zts ] ; then
			ln -sf /opt/${base_name}/usr/lib64/php-zts /opt/${base_name}/usr/lib/php-zts >/dev/null 2>&1
		fi
	else
		ln -sf /opt/${base_name}/usr/lib64 /opt/${base_name}/usr/lib >/dev/null 2>&1
	fi
fi

'cp' -f /opt/configs/php-fpm/etc/php-fpm.d/www.conf /opt/${base_name}/etc/php-fpm.d/www.conf >/dev/null 2>&1
